package db.dbTables;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import core.objects.ObjectWithID;
import core.objects.UserComment;

public class UserCommentTable extends AbstractDBTable {

    public static final String COLUMN_ID = "user_comment_id";
    public static final String COLUMN_COMMENTED_ID = "commented_id";
    public static final String COLUMN_COMMENTER_ID = "commenter_id";
    public static final String COLUMN_RATING = "rating";
    public static final String COLUMN_COMMENT = "comment";
    public static final String COLUMN_TIMESTAMP = "timestamp";

    final public DbColumn id = new DbColumn(COLUMN_ID, Types.INTEGER, 11, false, this);
    final public DbColumn commentedId = new DbColumn(COLUMN_COMMENTED_ID, Types.INTEGER, 11, false, this);
    final public DbColumn commenterId = new DbColumn(COLUMN_COMMENTER_ID, Types.INTEGER, 11, true, this);
    final public DbColumn rating = new DbColumn(COLUMN_RATING, Types.INTEGER, -1, false, this);
    final public DbColumn comment = new DbColumn(COLUMN_COMMENT, Types.VARCHAR, 140, true, this);
    final public DbColumn timestamp = new DbColumn(COLUMN_TIMESTAMP, Types.TIMESTAMP, 140, false, this);

    public UserCommentTable() {
        super("user_comment");
        setIDColumn(id);
    }

    @Override
    public List<DbColumn> getDbColumns() {
        List<DbColumn> list = new ArrayList<DbColumn>();
        list.add(id);
        list.add(commentedId);
        list.add(commenterId);
        list.add(rating);
        list.add(comment);
        list.add(timestamp);
        return list;
    }

    @Override
    public ObjectWithID convertSqlTableToBaseObject(ResultSet resultSet) throws SQLException {
        int id = resultSet.getInt(COLUMN_ID);
        int commentedId = resultSet.getInt(COLUMN_COMMENTED_ID);
        int commenterId = resultSet.getInt(COLUMN_COMMENTER_ID);
        int rating = resultSet.getInt(COLUMN_RATING);
        String comment = resultSet.getString(COLUMN_COMMENT);
        Timestamp timestamp = resultSet.getTimestamp(COLUMN_TIMESTAMP);

        return new UserComment(id, commenterId, commentedId, comment, rating, timestamp);
    }
}
